<!doctype html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<title>手势锁屏示例</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<!--标准mui.css-->
		<link rel="stylesheet" href="css/mui.min.css">
		<!--App自定义的css-->
		<link rel="stylesheet" type="text/css" href="css/app.css" />
		<script src="js/getURLParam.js"></script>
		<style>
			html,
			body {
				width: 100%;
				height: 100%;
				margin: 0px;
				padding: 0px;
				overflow: hidden;
				background-color: #efeff4;
			}
			#holder {
				width: 300px;
				height: 300px;
				border: solid 1px #bbb;
				border-radius: 5px;
				margin: 50px auto;
				/*background-color: #fff;*/
			}
			#alert {
				text-align: center;
				padding: 20px 10px;
			}
		</style>
	</head>

	<body  style="background-image: url(images/drawable-login/shoushi_bg.png);background-size: auto;width: 100%;height: 100%;margin: 0px;">
		<!--<header class="mui-bar mui-bar-nav">
			<h1 class="mui-title">解屏</h1>
		</header>-->
		<div style="margin-top: 50%;" >
			<div id='holder' class="mui-locker" style="border: none;" data-locker-options='{"ringColor":"rgba(210,210,210,1)","fillColor":"#ffffff","pointColor":"rgba(1,136,204,1)","lineColor":"rgba(0,136,204,1)"}' data-locker-width='300' data-locker-height='300'></div>
			<div id='alert'></div>
		</div>

		<script src="js/mui.min.js"></script>
		<script src="js/mui.locker.js"></script>
		<script>
		var winsetting = null;
		// 监听plusready事件  
		document.addEventListener( "plusready", function(){
			// 扩展API加载完毕，现在可以正常调用扩展API
			
				/*plus.statistic.eventStart( "test" );
			
				plus.statistic.eventEnd( "test" );*/
		
			
		}, false );
			(function($, doc) {
				$.init();
				var sessionid=GetQueryString("sessionid");
				var accountid=GetQueryString("accountId");
	    		var proversion=GetQueryString("proVersion");
	    		var isTruck=GetQueryString("isTruck");
	    		var conditionsstate=GetQueryString("conditionsstate");
	    		/*var sessionid="";
				var accountid="216";
	    		var proversion="1.0";
	    		var conditionsstate="1";*/
	    		var holder = doc.querySelector('#holder'),
					alert = doc.querySelector('#alert'),
					record = [];
	    		var errorcount=0;
	    		
					//获取手势密码的状态
				   $.ajax(getrequestURL()+'APP/handpwd', {
							data: {
								param: '{"proVersion":"' + proversion + '","accountId":"' + accountid + '","sessionid":"' + sessionid + '"}'
							},
							dataType: 'jsonp', //服务器返回json格式数据
							type: 'post', //HTTP请求类型
							timeout: 10000, //超时时间设置为10秒；
							success: function(data) {
								var jsonObj = window.JSON.parse(data);
								if (jsonObj.resultCode == "1") {
									//获取返回数据的data
									var resultdata = jsonObj.data;
									var s =document.getElementById("switchDiv");
									if (resultdata.pswdState=='1') {
										var localpassword = localStorage.getItem("locker");
										if(localpassword!=undefined && localpassword!=null){//本地记住手势密码
											holder.addEventListener('done',function(event){
												var rs = event.detail;
												if(errorcount==5){
													mui.toast("你输入的手势密码已错误5次，请重新登陆并设置手势密码！");
													localStorage.clear();
													$.openWindow("index.html");
													
												}else{
													if(rs.points.join('')==localpassword){
														window.location.href="offcanvas-drag-right.html?sessionid="+sessionid+"&accountId="+accountid+"&proVersion="+proversion+"&isTruck="+isTruck;
														//$.openWindow("offcanvas-drag-right.html?sessionid="+sessionid+"&accountId="+accountid+"&proVersion="+proversion);
													}else{
														var stste=4-errorcount;
														mui.toast("密码错误！请重新绘制！您还有"+stste+"次机会");
														errorcount++;
													}
												}
												
											});
										}else{//首次登陆
											//手势密码为开
											//处理事件
											var tryNumber = 5;
											holder.addEventListener('done', function(event) {
												var rs = event.detail;
												if (rs.points.length < 4) {
													alert.innerText = '至少连接四个点';
													record = [];
													rs.sender.clear();
													return;
												}
												console.log(rs.points.join(''));
												record.push(rs.points.join(''));
												if (record.length >= 2) {
													if (record[0] == record[1]) {
														alert.innerText = '手势设定完成';
														localStorage.setItem("locker",record[0]);
														if(conditionsstate==1){
															window.location.href="offcanvas-drag-right.html?sessionid="+sessionid+"&accountId="+accountid+"&proVersion="+proversion+"&isTruck="+isTruck;
													
														}else{
															window.location.href="serveClause.html?sessionid="+sessionid+"&accountId="+accountid+"&proVersion="+proversion+"&conditionsstate="+conditionsstate+"&isTruck="+isTruck;
													
														}
													} else {
														alert.innerText = '两次手势设定不一致';
													}
													rs.sender.clear();
													record = [];
												} else {
													alert.innerText = '请确认手势设定';
													rs.sender.clear();
												}
											});
										}
										
										var backButtonPress = 0;
										$.back = function(event) {
											backButtonPress++;
											if (backButtonPress > 1) {
												plus.runtime.quit();
											} else {
												plus.nativeUI.toast('再按一次退出应用');
											}
											setTimeout(function() {
												backButtonPress = 0;
											}, 1000);
											return false;
										};
									}else{//手势密码为关
											if(conditionsstate==1){
												window.location.href="offcanvas-drag-right.html?sessionid="+sessionid+"&accountId="+accountid+"&proVersion="+proversion+"&isTruck="+isTruck;		
											}else{
												window.location.href="serveClause.html?sessionid="+sessionid+"&accountId="+accountid+"&proVersion="+proversion+"&conditionsstate="+conditionsstate+"&isTruck="+isTruck;
													
											}
										}
								}
							}
						});
			}(mui, document));
		</script>
	</body>

</html>